﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.YouTube;
using Google.YouTube;
using System.Configuration;
using Google.GData.Client;
using BusinessLogic;
using BusinessLogic.Data;

namespace YoutubeAccess
{
    public class GoogleHelper
    {
        const string YOUTUBE_CHANNEL = "hibernatingrhinos";

        public Webinar[] SearchWebinarSite(DateTime? fromDate)
        {
            YouTubeRequestSettings settings = new YouTubeRequestSettings("rhinos channel", ConfigurationManager.AppSettings["devKey"]);
            YouTubeRequest request = new YouTubeRequest(settings);
            string feedUrl = String.Format("http://gdata.youtube.com/feeds/api/users/{0}/uploads?orderby=published", YOUTUBE_CHANNEL);

            FeedQuery query = new FeedQuery(feedUrl);

            if (!fromDate.HasValue)
            {
                fromDate = DateTime.MinValue;
            }
            else
            {
                fromDate = fromDate.Value.Subtract(new TimeSpan(1, 0, 0, 0));
            }

            Feed<Video> videoFeed = request.Get<Video>(query);
            return (from video in videoFeed.Entries
                    where video.YouTubeEntry.Published > fromDate
                    select new Webinar { UID = video.VideoId, Name = video.Title, Scheduled = video.YouTubeEntry.Published, URI = video.WatchPage.AbsoluteUri, Description = video.Description }).ToArray();
        }

    }
}
